
package com.epam.android.training.task;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

/**
 * @author pavel_busko
 * 
 * Activity for big interesting task.
 */
public class TaskActivity extends Activity {

    private static final int SPLASH_TIME = 3000;

    private static final String MIME_TYPE_TEXT = "plain/text";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getLastNonConfigurationInstance() == null) {
            setContentView(R.layout.splash);
            View splash = findViewById(R.id.splash);
            splash.postDelayed(new Runnable() {
                @Override
                public void run() {
                    setContentView(R.layout.main);
                    TextView textView = (TextView) findViewById(R.id.text);
                    textView.setText(R.string.hello);
                }
            }, SPLASH_TIME);
        } else {
            setContentView(R.layout.main);
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return Boolean.TRUE;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
            case R.id.developer_phone:
                intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:" + item.getTitle()));
                startActivity(intent);
                return true;
            case R.id.developer_email:
                intent = new Intent(Intent.ACTION_SEND);
                intent.setType(MIME_TYPE_TEXT);
                intent.putExtra(Intent.EXTRA_EMAIL, new String[] {
                    item.getTitle().toString()
                });
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    // if activity for this intent not found user will see
                    // standard message 'No app can perform this action'
                    startActivity(Intent.createChooser(intent,
                            getResources().getString(R.string.send_mail)));
                }
                return true;
            case R.id.news_item:
                Intent listActivityIntent = new Intent(getApplicationContext(), NewsActivity.class);
                startActivity(listActivityIntent);
                return true;
            case R.id.tests_item:
                Intent testListActivityIntent = new Intent(getApplicationContext(),
                        TestListActivity.class);
                startActivity(testListActivityIntent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
